import { useEffect, useState } from "react";
import { message } from "antd";
import { addUserSignInUsingPost } from "@/api/userController";
import { useSelector } from "react-redux";
import { RootState } from "@/stores";

/**
 * 添加用户刷题记录钩子
 * @param props
 * @constructor
 */
const UseAddUserSignInRecord = () => {
  // 加载状态
  const [loading, setLoading] = useState<boolean>(true);
  const loginUser = useSelector((state: RootState) => state.loginUser);

  const doFetch = async () => {
    setLoading(true);
    // 请求后端获取数据
    try {
      const res = await addUserSignInUsingPost({});
      localStorage.setItem(JSON.stringify(loginUser.id), JSON.stringify(res.data));
    } catch (e: any) {
      console.error(e);
      message.error("签到打卡失败，" + e.message);
    }
    setLoading(false);
  };

  // 保证只会调用一次
  useEffect(() => {
    doFetch();
  }, []);

  return { loading };
};

export default UseAddUserSignInRecord;
